﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hfm.Common.Entity.Contract;
using System.Data;
using System.Data.SqlClient;

namespace Hfm.Core.DataAccess.Contract
{
    public class ContractData
    {
        protected readonly CoreContext Context;
        protected ContractData()
        {

        }
        public ContractData(CoreContext context)
        {
            this.Context = context;
        }
        public void Create(ContractEntity contract)
        {
            Context.ContractEntityss.Add(contract);
            Context.SaveChanges();
        }

        /// <summary>
        /// 根据合同号查询表中是否含有相同订单号
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public bool IsExitesContractNumber(string number)
        {
            var query = from c in Context.ContractEntityss
                        where c.ContractNumber == number
                        select c;
            return query.ToList().Count > 0 ? true : false;
        }

        /// <summary>
        /// 通过id查询单一实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual ContractEntity GetContractById(int id)
        {
            return Context.ContractEntityss.Find(id); 
        }

        /// <summary>
        /// 添加一个实体
        /// </summary>
        /// <param name="Contract"></param>
        public virtual void CreateContract(ContractEntity Contract)
        {
            Context.ContractEntityss.Add(Contract);
            Context.SaveChanges();
        }

        /// <summary>
        /// 根据id删除实体
        /// </summary>
        /// <param name="id"></param>
        public virtual void DeleteContract(int id)
        {
            string sql = "delete from Contract where ID=@id";
            List<SqlParameter> sp = new List<SqlParameter>();
            sp.Add(new SqlParameter("@id", id));
            Context.Database.ExecuteSqlCommand(sql, sp.ToArray());
        }
        /// <summary>
        /// 修改一个实体
        /// </summary>
        /// <param name="Contract"></param>
        public virtual void UpdateContract(ContractEntity Contract)
        {
            Context.ContractEntityss.Attach(Contract);
            Context.Entry(Contract).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
        }

        /// <summary>
        /// 查询符合条件的实体总数
        /// </summary>
        /// <param name="Customer"></param>
        /// <param name="ContractName"></param>
        /// <param name="ContractNumber"></param>
        /// <returns></returns>
        public virtual int GetContactsCount(string Customer = null,string ContractName = null,string ContractNumber = null)
        {
            var query = from c in Context.ContractEntityss
                        join com in Context.CustomerEntities
                        on c.CustomerID equals com.Id into temp
                        from com in temp.DefaultIfEmpty()
                        select new NewContractEntity
                        {
                            ID = c.ID,
                            Customer = (com == null ? "" : com.CustomerName),
                            ContractName = c.ContractName,
                            ContractNumber = c.ContractNumber,
                            StartData = c.StartData,
                            ContractPeriod = c.ContractPeriod,
                        };

            if (!string.IsNullOrWhiteSpace(Customer))
            {
                query = query.Where(c => c.Customer.Contains(Customer));
            }

            if (!string.IsNullOrWhiteSpace(ContractName))
            {
                query = query.Where(c => c.ContractName.Contains(ContractName));
            }
            if (!string.IsNullOrWhiteSpace(ContractNumber))
            {
                query = query.Where(c => c.ContractNumber.Contains(ContractNumber));
            }

            return query.Count();
        }

        /// <summary>
        /// 查询符合条件的实体信息
        /// </summary>
        /// <param name="Customer"></param>
        /// <param name="ContractName"></param>
        /// <param name="ContractNumber"></param>
        /// <param name="skip"></param>
        /// <param name="take"></param>
        /// <returns></returns>
        public virtual IList<NewContractEntity> GetContacts(string Customer = null, string ContractName = null, string ContractNumber = null, int? skip = null, int? take = null)
        {
            if ((skip == null) ^ (take == null))
                throw new ArgumentException("Skip and take parameter must be provided together or left null.");

            var query = from c in Context.ContractEntityss
                         join com in Context.CustomerEntities
                         on c.CustomerID equals com.Id into temp
                         from com in temp.DefaultIfEmpty()
                        select new NewContractEntity
                        {
                            ID = c.ID,
                            Customer = (com == null ? "" : com.CustomerName),
                            ContractName = c.ContractName,
                            ContractNumber = c.ContractNumber,
                            StartData = c.StartData,
                            ContractPeriod = c.ContractPeriod,
                        };

            if (!string.IsNullOrWhiteSpace(Customer))
            {
                query = query.Where(c => c.Customer.Contains(Customer));
            }

            if (!string.IsNullOrWhiteSpace(ContractName))
            {
                query = query.Where(c => c.ContractName.Contains(ContractName));
            }
            if (!string.IsNullOrWhiteSpace(ContractNumber))
            {
                query = query.Where(c => c.ContractNumber.Contains(ContractNumber));
            }

            query = query.OrderBy(c => c.ID);

            if (skip != null && take != null)
            {
                query = query.Skip(skip ?? 0).Take(take ?? int.MaxValue);
            }
            return query.ToList();
        }
    }
}
